package com.example.devinfan.criminalintent.Fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.devinfan.criminalintent.R;

import java.util.Date;


public class ChangeTimeFragment extends DialogFragment {

    private Button mDateButton;
    private Button mTimeButton;
    private TextView mTimeTextView;
    private Date mDate;
    public static final String EXTRA_CHANGE_DATE = "com.example.devinfan.criminalintent.changeTime";
    private static final String DIALOG_CHANGE_DATE = "changeDate";
    private static final String DIALOG_CHANGE_TIME = "changeTime";
    private static final int REQUEST_CHANGE_DATE = 0;
    private static final int REQUEST_CHANGE_TIME = 1;
    private static final String KEY_INDEX = "date";
    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceSate){
        mDate = (Date)getArguments().getSerializable(EXTRA_CHANGE_DATE);
        //setRetainInstance(true);
        if(savedInstanceSate != null) {
            mDate = (Date) savedInstanceSate.getSerializable(KEY_INDEX);
        }

        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_changetime,null);

        mDateButton = (Button)view.findViewById(R.id.crime_date_change);
        mTimeButton = (Button)view.findViewById(R.id.crime_time_change);
        mTimeTextView = (TextView)view.findViewById(R.id.crime_time_TextView);

        mTimeTextView.setText(mDate.toString());
        mTimeButton.setEnabled(true);
        mDateButton.setEnabled(true);
        mTimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                TimePickerFragment dialog = TimePickerFragment.newInstance(mDate);
                dialog.setTargetFragment(ChangeTimeFragment.this,REQUEST_CHANGE_TIME);
                dialog.show(fm,DIALOG_CHANGE_TIME);
                mTimeButton.setEnabled(false);
                mDateButton.setEnabled(false);
            }
        });

        mDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                DatePickerFragment dialog = DatePickerFragment.newInstance(mDate);
                dialog.setTargetFragment(ChangeTimeFragment.this, REQUEST_CHANGE_DATE);
                dialog.show(fm,DIALOG_CHANGE_DATE);
                mTimeButton.setEnabled(false);
                mDateButton.setEnabled(false);
            }
        });

        return new AlertDialog.Builder(getActivity())
                .setView(view)
                .setTitle(R.string.change_time_title)
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                sendResult(Activity.RESULT_OK);
                            }
                        })
                .create();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if (resultCode != Activity.RESULT_OK) return ;
        if (requestCode == REQUEST_CHANGE_DATE){
            mDate = (Date)data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
            mTimeTextView.setText(mDate.toString());
        }else if (requestCode == REQUEST_CHANGE_TIME){
            mDate = (Date)data.getSerializableExtra(TimePickerFragment.EXTRA_TIME);
            mTimeTextView.setText(mDate.toString());
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putSerializable(KEY_INDEX,mDate);
    }

    public static ChangeTimeFragment newInstance(Date date){
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_CHANGE_DATE,date);

        ChangeTimeFragment fragment = new ChangeTimeFragment();
        fragment.setArguments(args);

        return fragment;
    }

    private void sendResult(int resultCode){
        if (getTargetFragment() == null){
            return ;
        }

        Intent intent = new Intent();
        intent.putExtra(EXTRA_CHANGE_DATE,mDate);

        getTargetFragment().onActivityResult(getTargetRequestCode(),resultCode,intent);
    }
}
